package com.qiezi.base;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.google.android.exoplayer2.ui.PlayerNotificationManager;
import com.qiezi.R;
import com.qiezi.util.FontUtils;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import static android.support.v4.content.ContextCompat.getColor;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    public static final int TYPE_FOOTER = 2;
    public static final int TYPE_EMPTY = 3;

    private ArrayList<T> mDatas = new ArrayList<>();

    private View mHeaderView;
    private View mFooterView;
    private View mEmptyView;

    private boolean isSigleFooter = true;



    public void setFooterText(String text){
        if (mFooterView == null)return;
        if (mFooterView instanceof TextView){
            ((TextView) mFooterView).setText(text);
            if (mFooterView.getVisibility() == View.INVISIBLE){
                mFooterView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setFooterViewDef(BaseMvpActivity activity){
        setFooterView(getFooter(activity));
    }

    private TextView getFooter(BaseMvpActivity activity){

        TextView tvFooter = new TextView(activity);

        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        tvFooter.setLayoutParams(params);
        tvFooter.setGravity(Gravity.CENTER);
        tvFooter.setVisibility(View.INVISIBLE);
        tvFooter.setTextSize(14);
        tvFooter.setTextColor(getColor(activity,R.color.colorGray));
        return tvFooter;
    }

    private RelativeLayout rlEmpty;

    public void setEmptyViewDef(Context activity, int imageID){
        if (rlEmpty == null) {
            rlEmpty = getEmptyView(activity,imageID);
        }
        setEmptyView(rlEmpty);
    }

    public void setEmptyImage(int id){
        if (rlEmpty == null || ivEmpty == null)return;
        ivEmpty.setImageResource(id);
    }

    public void setEmptyText(CharSequence str){
        if (rlEmpty == null || tvEmpty == null)return;
        System.out.println("setEmptyText");
        tvEmpty.setText(str);
    }

    public int getEmptyVisibility(){
        if (rlEmpty == null)return 0;
        return rlEmpty.getVisibility();
    }
    public void setEmptyVisibility(int visibility){
        if (rlEmpty == null)return;
        rlEmpty.setVisibility(visibility);
    }

    private ImageView ivEmpty;
    private TextView tvEmpty;

    private RelativeLayout getEmptyView(Context activity,int imageID){

        RelativeLayout empty = new RelativeLayout(activity);
        empty.setVisibility(View.INVISIBLE);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,RecyclerView.LayoutParams.MATCH_PARENT);
        empty.setLayoutParams(params);

        LinearLayout linearLayout = new LinearLayout(activity);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params1.addRule(RelativeLayout.CENTER_VERTICAL);
        params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
        linearLayout.setLayoutParams(params1);
        linearLayout.setGravity(Gravity.CENTER);
        empty.addView(linearLayout);


        ivEmpty = new ImageView(activity);
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,FontUtils.dip2px(activity,120));
        ivEmpty.setLayoutParams(params2);
        ivEmpty.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        if (imageID == -1){
            ivEmpty.setVisibility(View.GONE);
        }else {
            ivEmpty.setImageDrawable(ContextCompat.getDrawable(activity,imageID));
        }


        linearLayout.addView(ivEmpty);


        tvEmpty = new TextView(activity);
        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        tvEmpty.setLayoutParams(params3);
        tvEmpty.setTextSize(14);
        tvEmpty.setTextColor(0xff333333);
        linearLayout.addView(tvEmpty);

        return empty;
    }

    public View getFooterView() {
        return mFooterView;
    }

    public void setFooterView(View mFooterView) {
        this.mFooterView = mFooterView;
        notifyItemInserted(getItemCount());
    }

    public boolean isSigleFooter() {
        return isSigleFooter;
    }

    public void setSigleFooter(boolean sigleFooter) {
        isSigleFooter = sigleFooter;
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener<T> li) {
        mListener = li;
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    public View getEmptyView() {
        return mEmptyView;
    }

    public void setEmptyView(View mEmptyView) {
        this.mEmptyView = mEmptyView;
        notifyItemInserted(0);
    }

    public void setDatas(ArrayList<T> datas) {//刷新数据
        if (datas == null){
            Log.d(TAG, "addDatas: datas null");
            return;
        }
        //数据去重
        for (int i = 0; i<datas.size(); i++){
            if (mDatas.contains(datas.get(i))){
                datas.remove(i--);
            }
        }
        if (datas.size()>0){
            mDatas.addAll(0,datas);
            removeRepeat(true);
            notifyDataSetChanged();
        }
    }

    private int dataMaxSize = 100;

    private void removeRepeat(boolean isLastRemove){//保存数据长度为100
        int len = mDatas.size()-dataMaxSize;
        if (len>0){
            int re = isLastRemove ? dataMaxSize-1 : 0;

            for (int i=0; i<len;i++)
                mDatas.remove(re);
        }
    }

    private String TAG = "baseRecycler";

    public void addDatas(ArrayList<T> datas) {//追加数据
        if (datas == null){
            Log.d(TAG, "addDatas: datas null");
            return;
        }
        mDatas.addAll(datas);
        removeRepeat(false);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if(mEmptyView != null && mDatas.size() == 0)return TYPE_EMPTY;
        if(mHeaderView == null && mFooterView == null) return TYPE_NORMAL;
        if(position == 0 && mHeaderView != null) return TYPE_HEADER;
        if(position == getItemCount()-1 && mFooterView != null) return TYPE_FOOTER;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        if(mEmptyView != null && viewType == TYPE_EMPTY) return new Holder(mEmptyView);
        if(mHeaderView != null && viewType == TYPE_HEADER) {
            Log.d(TAG, "onCreateViewHolder: header");
            return new Holder(mHeaderView);
        }
        if(mFooterView != null && viewType == TYPE_FOOTER) return new Holder(mFooterView);
        return onCreate(parent, viewType);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {


        int type = getItemViewType(position);
        if(type == TYPE_HEADER || type == TYPE_FOOTER || type == TYPE_EMPTY) return;

        final int pos = getRealPosition(viewHolder);
        final T data = mDatas.get(pos);
        onBind(viewHolder, pos, data);

        if(mListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(pos, data);
                }
            });
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    return type == TYPE_EMPTY || type == TYPE_HEADER || (type == TYPE_FOOTER && isSigleFooter)
                            ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if(lp != null
                && lp instanceof StaggeredGridLayoutManager.LayoutParams
                && holder.getLayoutPosition() == 0) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(true);
        }
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        if (mEmptyView != null && mDatas.size() == 0) return 1;
        if (mHeaderView == null && mFooterView == null){
            return mDatas.size();
        }else if (mHeaderView != null && mFooterView != null){
            return mDatas.size() + 2;
        }else {
            return mDatas.size() + 1;
        }
    }



    public ArrayList<T> getData(){
        return mDatas;
    }

    public int getDataSize(){
        return mDatas.size();
    }

    public abstract RecyclerView.ViewHolder onCreate(ViewGroup parent, final int viewType);
    public abstract void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, T data);

    public static class Holder extends RecyclerView.ViewHolder {
        public Holder(View itemView) {
            super(itemView);
        }
    }

    public interface OnItemClickListener<T> {
        void onItemClick(int position, T data);
    }
}
